package com.kexio.sms.entity;

import java.time.LocalDateTime;

/**
 * 验证码实体 (存储在Redis中)
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public class VerificationCode {
    
    /**
     * 手机号码
     */
    private String phone;
    
    /**
     * 验证码
     */
    private String code;
    
    /**
     * 验证码类型 (login, register, reset_password, modify_phone)
     */
    private String type;
    
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    
    /**
     * 过期时间
     */
    private LocalDateTime expireTime;
    
    /**
     * 验证次数
     */
    private Integer verifyCount;
    
    /**
     * 最大验证次数
     */
    private Integer maxVerifyCount;
    
    /**
     * 是否已使用
     */
    private Boolean used;
    
    /**
     * IP地址
     */
    private String ipAddress;
    
    /**
     * 用户代理
     */
    private String userAgent;
    
    /**
     * 业务类型
     */
    private String businessType;
    
    // Constructors
    public VerificationCode() {}
    
    public VerificationCode(String phone, String code, String type, int expireMinutes) {
        this.phone = phone;
        this.code = code;
        this.type = type;
        this.createTime = LocalDateTime.now();
        this.expireTime = LocalDateTime.now().plusMinutes(expireMinutes);
        this.verifyCount = 0;
        this.maxVerifyCount = 3; // 默认最多验证3次
        this.used = false;
    }
    
    /**
     * 检查是否过期
     */
    public boolean isExpired() {
        return LocalDateTime.now().isAfter(expireTime);
    }
    
    /**
     * 检查是否超过最大验证次数
     */
    public boolean isMaxVerifyCountExceeded() {
        return verifyCount >= maxVerifyCount;
    }
    
    /**
     * 增加验证次数
     */
    public void incrementVerifyCount() {
        this.verifyCount++;
    }
    
    /**
     * 标记为已使用
     */
    public void markAsUsed() {
        this.used = true;
    }
    
    /**
     * 检查验证码是否有效 (未过期、未使用、未超过验证次数)
     */
    public boolean isValid() {
        return !isExpired() && !used && !isMaxVerifyCountExceeded();
    }
    
    // Getters and Setters
    public String getPhone() { return phone; }
    public void setPhone(String phone) { this.phone = phone; }
    
    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
    
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    
    public LocalDateTime getCreateTime() { return createTime; }
    public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }
    
    public LocalDateTime getExpireTime() { return expireTime; }
    public void setExpireTime(LocalDateTime expireTime) { this.expireTime = expireTime; }
    
    public Integer getVerifyCount() { return verifyCount; }
    public void setVerifyCount(Integer verifyCount) { this.verifyCount = verifyCount; }
    
    public Integer getMaxVerifyCount() { return maxVerifyCount; }
    public void setMaxVerifyCount(Integer maxVerifyCount) { this.maxVerifyCount = maxVerifyCount; }
    
    public Boolean getUsed() { return used; }
    public void setUsed(Boolean used) { this.used = used; }
    
    public String getIpAddress() { return ipAddress; }
    public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; }
    
    public String getUserAgent() { return userAgent; }
    public void setUserAgent(String userAgent) { this.userAgent = userAgent; }
    
    public String getBusinessType() { return businessType; }
    public void setBusinessType(String businessType) { this.businessType = businessType; }
}
